﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Globalization;

namespace FlexiCommerce.Web.Admin.DynamicData.FieldTemplates
{
    public partial class Culture_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            // use linq to get a list of cultures to display in the drop down list
            var cultures = from c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                           select new
                           {
                               Value = c.Name,
                               Name = c.Name + " - " + c.DisplayName
                           };

            // setup the drop down list
            DropDownList1.DataSource = cultures;
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataBind();
        }

        protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
            // check the FieldValueString is not null
            if (String.IsNullOrEmpty(FieldValueString))
            {
                // set it to the culture of the client session
                DropDownList1.SelectedValue = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
            }
            else
            {
                // set the drop down list to the current vlaue
                DropDownList1.SelectedValue = FieldValueString;
            }
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            // get selected value
            dictionary[Column.Name] = FlexiCommerce.Components.Culture.FromCultureInfo(new CultureInfo(DropDownList1.SelectedValue));
        }

        public override Control DataControl
        {
            get
            {
                return DropDownList1;
            }
        }

    }
}
